<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Commercial_objective_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $commercial_id = 0;
	var $commercial_username = '';
	var $commercial_name = '';
	var $rate_from = 0;
	var $rate_to = 0;
	var $actual_registered = 0;
	var $price_per_register = 0;
	var $created_time = 0;
	var $active = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setCommercialId($input){
		$this->commercial_id = $input;
	}
	function setCommercialUsername($input){
		$this->commercial_username = $input;
	}
	function setCommercialName($input){
		$this->commercial_name = $input;
	}
	function setRateFrom($input){
		$this->rate_from = $input;
	}
	function setRateTo($input){
		$this->rate_to = $input;
	}
	function setActualRegistered($input){
		$this->actual_registered = $input;
	}
	function setPricePerRegister($input){
		$this->price_per_register = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	function setActive($input){
		$this->active = $input;
	}
	function getId(){
		return $this->id;
	}
	function getCommercialId(){
		return $this->commercial_id;
	}
	function getCommercialUsername(){
		return $this->commercial_username;
	}
	function getCommercialName(){
		return $this->commercial_name;
	}
	function getRateFrom(){
		return $this->rate_from;
	}
	function getRateTo(){
		return $this->rate_to;
	}
	function getActualRegistered(){
		return $this->actual_registered;
	}
	function getPricePerRegister(){
		return $this->price_per_register;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	function getActive(){
		return $this->active;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_COMMERCIAL_OBJECTIVE)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->commercial_id = $result[0]->commercial_id;
			$this->commercial_name = $result[0]->commercial_name;
			$this->commercial_username = $result[0]->commercial_username;
			$this->rate_from = $result[0]->rate_from;
			$this->rate_to = $result[0]->rate_to;
			$this->actual_registered = $result[0]->actual_registered;
			$this->price_per_register = $result[0]->price_per_register;
			$this->created_time = $result[0]->created_time;
			$this->active = $result[0]->active;
			return $this;
		}
		return false;
	}
	
	function getList($username = '', $limit = array()){
		$ci=& get_instance();
		if($username){
			$ci->db->where('commercial_username', $username);
		}
		if($limit && count($limit) > 0){
			$ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time', 'DESC');
		return $ci->db->get(TBL_COMMERCIAL_OBJECTIVE)->result();
	}
	function count(){
		$ci=& get_instance();
		$ci->db->where('active', 1);
		return $ci->db->count_all_results(TBL_COMMERCIAL_OBJECTIVE);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set('commercial_id',$this->getCommercialId());
		$ci->db->set('commercial_name',$this->getCommercialName());
		$ci->db->set('commercial_username',$this->getCommercialUsername());
		$ci->db->set('rate_from',$this->getRateFrom());
		$ci->db->set('rate_to',$this->getRateTo());
		$ci->db->set('actual_registered',$this->getActualRegistered());
		$ci->db->set('price_per_register',$this->getPricePerRegister());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		$ci->db->insert(TBL_COMMERCIAL_OBJECTIVE);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('commercial_id',$this->getCommercialId());
		$ci->db->set('commercial_name',$this->getCommercialName());
		$ci->db->set('commercial_username',$this->getCommercialUsername());
		$ci->db->set('rate_from',$this->getRateFrom());
		$ci->db->set('rate_to',$this->getRateTo());
		$ci->db->set('actual_registered',$this->getActualRegistered());
		$ci->db->set('price_per_register',$this->getPricePerRegister());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		return $ci->db->where('id', $this->id)->update(TBL_COMMERCIAL_OBJECTIVE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_COMMERCIAL_OBJECTIVE);
	}
	public function countCompletedObjective(){
		$ci=& get_instance();
		$result = $ci->db->get_where(TBL_COMMERCIAL_OBJECTIVE, 'actual_registered >= rate_from AND active = 1')->result();
		//print_r($ci->db);
		if($result){
			return count($result);
		}else{
			return 0;
		}
	}
}